package com.twotiger.and.activity.account.invested;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.adapter.DealRecordAdapter;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.DataList;
import com.twotiger.and.bean.Invest;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.PreferencesUtils;
import com.twotiger.and.util.TimeUtils;
import com.twotiger.and.util.ViewUtils;
import com.view.pulltorefresh.MyListView;
import com.view.pulltorefresh.PullToRefreshBase;
import com.view.pulltorefresh.PullToRefreshBase.OnLastItemVisibleListener;
import com.view.pulltorefresh.PullToRefreshMyListView;

/**
 * 
 * @Description: 
 * 投资记录 
 * @Author douxy
 * @Date 2015年8月12日 下午1:57:17
 */
public class InverstedRecordPage extends BaseActivity implements OnClickListener {

    private static final String TAG_refresh = "DealRecordPage_TAG_refresh";
    private static final int REPHRESHSUCCES = 0;
    private static final int REPHRESHFAIL = 1;
    
    public static final String REQTYPE_CYZ = "cyz";
    public static final String REQTYPE_YHK = "yhk";
    
    private PullToRefreshMyListView deal_record_refresh_list;
    private ArrayList list;// 按月排序分组
    private DealRecordAdapter dealRecordAdapter;
    private int pNum = 1;
    private View no_content_rl;
    private HashMap<String, String> params;
    private SubTitle subTitle;

    public String type;
    private ImageView iv_left,iv_right;
    private RadioButton rb_not_complete,rb_complete;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.deal_record_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        deal_record_refresh_list = (PullToRefreshMyListView) view
            .findViewById(R.id.deal_record_refresh_list);
        no_content_rl = (View) view.findViewById(R.id.no_content_rl);
        iv_left = (ImageView) view.findViewById(R.id.iv_left);
        iv_right = (ImageView) view.findViewById(R.id.iv_right);
        rb_not_complete = (RadioButton) view.findViewById(R.id.rb_not_complete);
        rb_complete = (RadioButton) view.findViewById(R.id.rb_complete);
        
        
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("直投项目");
        subTitle.hideBtns();
        subTitle.hideBottomLine();
        tintManager.setTintResource(R.drawable.bg_account_statusbar);
        subTitle.setBgResource(R.drawable.bg_account_title);
        subTitle.setColor(getResources().getColor(color.white));
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
        subTitle.title_left_bt.setImageResource(R.drawable.back_w);
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.rb_not_complete:
            pNum = 1;
            type = REQTYPE_CYZ;
            rb_not_complete.setTextColor(getResources().getColor(color.orange));
            rb_complete.setTextColor(getResources().getColor(color.home_bottom));
            iv_left.setVisibility(View.VISIBLE);
            iv_right.setVisibility(View.GONE);

            refreshlist();
            break;
        case R.id.rb_complete:
            pNum = 1;
            type = REQTYPE_YHK;
            rb_complete.setTextColor(getResources().getColor(color.orange));
            rb_not_complete.setTextColor(getResources().getColor(color.home_bottom));
            iv_right.setVisibility(View.VISIBLE);
            iv_left.setVisibility(View.GONE);

            refreshlist();
            break;
        case R.id.no_content_rl:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            refreshlist();
            break;
        default:
            break;
        }
    }

    @Override
    protected void setListener() {
        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case REPHRESHSUCCES:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        deal_record_refresh_list.getLoadingLayoutProxy().setLastUpdatedLabel(
                            "最后更新：今天 " + TimeUtils.getFormatDate(" HH:mm"));
                        DataList dataList = JSONObject.parseObject(result.data, DataList.class);
                        application.setDealRecord_timestamp(dataList.getTimestamp());
                        List<Invest> invests = JSONArray.parseArray(dataList.getList(),
                            Invest.class);
                        if (!ListUtils.isEmpty(invests)) {
                            if(invests.size() < Integer.parseInt(Constants.pageSize)){
                                deal_record_refresh_list.changeMyFooterViewContent(false);
                            }else{
                                deal_record_refresh_list.changeMyFooterViewContent(true);
                            }
                            no_content_rl.setVisibility(View.GONE);
                            deal_record_refresh_list.setVisibility(View.VISIBLE);
                            if (pNum == 1) {
                                dealRecordAdapter.resetData(invests,type);
                            } else {
                                dealRecordAdapter.addData(invests,type);
                            }

                        } else {
                            if (pNum == 1) {
                                no_content_rl.setVisibility(View.VISIBLE);
                            } else {
                                deal_record_refresh_list.changeMyFooterViewContent(false);
                                dealRecordAdapter.notifyDataSetChanged();
                            }
                        }
                    } else {
                        showToast(result.codeDesc);
                    }
                    deal_record_refresh_list.onRefreshComplete();

                    break;
                case REPHRESHFAIL:
                    deal_record_refresh_list.onRefreshComplete();
                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
        
        deal_record_refresh_list.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                String footerContent = ((TextView)deal_record_refresh_list.getChildviewOfFooterview()).getText().toString();
                if("已无更多项目".equals(footerContent)){
                    return;
                }
                /**
                 * 加载更多数据
                 */
                pNum++;
                refreshlist();
            }
        });
        deal_record_refresh_list
            .setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<MyListView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<MyListView> refreshView) {
                    /**
                     * 刷新数据
                     */
                    pNum = 1;
                    refreshlist();
                }

                @Override
                public void onPullUpToRefresh(PullToRefreshBase<MyListView> refreshView) {
                    /**
                     * 加载更多数据
                     */
                    pNum++;
                    refreshlist();
                }

            });
        
        no_content_rl.setOnClickListener(this);
        iv_left.setOnClickListener(this);
        iv_right.setOnClickListener(this);
        rb_not_complete.setOnClickListener(this);
        rb_complete.setOnClickListener(this);
    }

    private void refreshlist() {
        params.clear();
        params.put("token", getToken());
        params.put("pageNum", String.valueOf(pNum));
        params.put("pageSize", Constants.pageSize);
        params.put("type", type);
        params.put("timestamp", TimeUtils.getTimeStamp());
        getDate(params, Constants.INVESTQUERY, handler, REPHRESHSUCCES, REPHRESHFAIL, true,
            true, false);
    }

    @Override
    protected void processLogic() {
        type = REQTYPE_CYZ;
        params = getParm();
        list = new ArrayList();
        dealRecordAdapter = new DealRecordAdapter(this, list);
        deal_record_refresh_list.setAdapter(dealRecordAdapter);
        deal_record_refresh_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        initData();
    }

    public void initData() {
        refreshlist();
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }
}
